@extends('admin.layouts')
@section('style')
    <link rel="stylesheet" type="text/css" href="{{asset('public/js/minicolors/jquery.minicolors.css')}}">
    <style>
        html,body{
            height:100%;
        }
    </style>
@endsection
@section('main')

<form class="layui-form luck-pane" id="form1" method="post">
    <div class="luck-content clearfix">
        <div class="layer-box">
            <div class="layui-tab-content layui-form-pane">
                    <div class="layui-tab-item layui-show">
                        {{csrf_field()}}
                        <div class="layui-form-item">
                            <label class="luck-form-label">{{trans('config.column')}}</label>
                            <div class="layui-input-inline">
                                <select name="cid" lay-verify="required">
                                    <option value="0">{{trans('config.all')}}</option>
                                    @foreach($cates as $v)
                                        <option value="{{$v->id}}" >{{$v->_cate_name}}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="luck-form-label">{{trans('config.name')}}</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" required jq-verify="required" jq-error="{{ trans('config.vali_required') }}" lay-verify="new_required" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="luck-form-label">{{trans('config.color')}}</label>
                            <div class="layui-input-inline">
                                <input type="text" name="color" class="layui-input luck-color" id="color">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="luck-form-label">{{trans('config.description')}}</label>
                            <div class="layui-input-inline">
                                <textarea name="description" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item" id="img" style="display: none;">
                            <label class="luck-form-label"></label>
                            <div class="layui-input-inline">
                                <img src="" style="max-width:100%;max-height:150px;" />
                                <input type="text" name="image" class="layui-input" style="display: none;">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="luck-form-label">{{trans('config.image')}}</label>
                            <div class="layui-input-inline">
                                <input type="file" name="file" class="layui-upload-file" lay-title="{{trans('config.add')}}{{trans('config.category')}}{{trans('config.image')}}">
                            </div>
                        </div>
                        <div class="layui-form-item" >
                            <label class="luck-form-label">{{trans('config.order')}}</label>
                            <div class="layui-input-inline">
                                <input type="text" name="listsort" placeholder="{{trans('config.default')}}: 0" class="layui-input">
                            </div>
                        </div>
                    </div>
            </div>
        </div>
    </div>
    <div class="luck-footer luck-footer-right luck-footer-padding">
        <div class="luck-block">
            <button class="layui-btn" lay-submit lay-filter="sub">{{trans('config.btn_submit')}}</button>
            <button class="layui-btn layui-btn-primary" type="button" id="close">{{trans('config.btn_close')}}</button>
        </div>
    </div>
</form>

@endsection

@section('script')
    <script src="{{asset('public/js/jquery.min.js')}}"></script>
    <script src="{{asset('public/js/minicolors/jquery.minicolors.min.js')}}"></script>
<script>
    $(document).ready( function() {
        $("#color").minicolors();
    })
    layui.use(['jquery','myform','layer','upload'],function(){
        var $= layui.jquery;
        var index = parent.layer.getFrameIndex(window.name);
        var form = layui.form();
        var load;
        layui.upload({
            url: '{{url("admin/upload")}}?type=attr&_token={{csrf_token()}}' //上传接口
            ,before: function(){
                load=layer.load(1, {
                    shade: [0.1,'#fff']
                });
            },success: function(res){ //上传成功后的回调
                if(res.status==200){
                    $("#img").show().find('img').attr('src','/'+res.data.src);
                    $("input[name=image]").val(res.data.src);
                    layer.msg(res.msg,{icon:1,shift:5});
                }else{
                    layer.msg(res.msg,{icon: 5,shift:6});
                }
                layer.close(load);
            }
        });

        form.verify({
            new_url:function(value){
                if(!/(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/.test(value) && !this.new_required(value)){
                    return "{{ trans('config.vali_url') }}";
                }
            },
            new_required:function(value){
                if(!/[\S]+/.test(value)){
                    return "{{ trans('config.vali_required') }}";
                }
            }
        });
        $('#close').click(function(){
            parent.layer.close(index);
        });

        form.on('submit(sub)', function(data){
            $.ajax({
                url:"{{url('admin/attradd')}}",
                type:"post",
                data:data.field,
                beforeSend:function(){
                    load=layer.load(1, {
                        shade: [0.1,'#fff']
                    });
                },
                success:function(data){
                    if(data.status==200){
                        layer.msg(data.msg,{icon:1,shift:5});
                        parent.layer.close(index);
                        if(data.url=="reload"){
                            parent.location.reload();
                        }
                    }else{
                        layer.msg(data.msg,{icon: 5,shift:6});
                    }
                },
                complete:function(){
                    layer.close(load);
                },
                error:function(){
                    layer.msg("{{trans('config.connect')}}",{icon: 5,shift:6});
                }
            })
            return false;
        });
    });
</script>
@endsection